package com.sukaiyi.giteeideaplugin.cellrenderer;

import com.intellij.icons.AllIcons;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.sukaiyi.giteeideaplugin.model.GitFileNode;
import com.sukaiyi.giteeideaplugin.utils.FileTypeUtils;
import icons.PluginIcons;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.Objects;

public class GitFileTreeCellRenderer extends ColoredTreeCellRenderer {

    @Override
    public void customizeCellRenderer(@NotNull JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        if (value instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
            Object userObject = treeNode.getUserObject();
            if (userObject == null || userObject.getClass() != GitFileNode.class) {
                return;
            }
            GitFileNode node = (GitFileNode) userObject;
            String type = node.getType();
            String path = node.getPath();
            if (Objects.equals(type, "tree")) {
                setIcon(AllIcons.Nodes.Folder);
            } else if (Objects.equals(type, "blob")) {
                setIcon(FileTypeUtils.getIconForName(path));
            } else {
                setIcon(PluginIcons.REPO_BLUE);
            }
            append(node.getPath());
        }
    }
}